package com.yanqu.road.server.manger.rank;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.rank.UserRank;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.logic.rank.RankListModel;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.cross.CrossDataMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigInteger;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ActivityRankMgr extends TempMgr {
    private static Logger logger = LogManager.getLogger(ActivityRankMgr.class.getName());
    private static Object locker = new Object();
    /**
     * 活动榜单，活动编号 类型 榜单
     */
    private static Map<Integer, Map<Integer, RankListModel>> activityRankListModelMap = new ConcurrentHashMap<>();
    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }
    @Override
    public boolean reloadData() throws Exception {
        activityRankListModelMap = new ConcurrentHashMap<>();
        reloadActivityRank();
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    /**
     * 重新载入活动榜单
     */
    public static void reloadActivityRank() {
        for (ActivityInfo activityInfo : NormalActivityMgr.getNewRankActivityInfoList()) {// 加载冲榜活动
            Map<Integer, List<ActivityConditionInfo>> conditionInfoMap = NormalActivityMgr.getActivityConditionInfoMap(activityInfo.getActivityId());
            for(Map.Entry<Integer, List<ActivityConditionInfo>> dataEntry : conditionInfoMap.entrySet()) {
                if (!activityRankListModelMap.containsKey(activityInfo.getActivityId())) {
                    synchronized (activityRankListModelMap) {
                        if(!activityRankListModelMap.containsKey(activityInfo.getActivityId())) {
                            activityRankListModelMap.put(activityInfo.getActivityId(), new ConcurrentHashMap<>());
                        }
                    }
                }
                Map<Integer, RankListModel> modelMap = activityRankListModelMap.get(activityInfo.getActivityId());
                if(!modelMap.containsKey(dataEntry.getKey())){
                    synchronized (modelMap){
                        if(!modelMap.containsKey(dataEntry.getKey())){
                            RankListModel rankListModel;
                            Comparator comparator = ActivityHelper.getUserRankSorter(activityInfo.getType(), dataEntry.getKey(), false);
                            if(comparator == null){
                                rankListModel = new RankListModel(dataEntry.getKey(), activityInfo.getActivityId());
                            }else {
                                rankListModel = new RankListModel(dataEntry.getKey(), activityInfo.getActivityId(), comparator);
                            }
                            int rankNum = ActivityHelper.getMaxRank(dataEntry.getValue());
                            if (rankNum <= 0) {
                                rankNum = GameConfig.RANK_DEFAULT_MAX_COUNT;
                            }
                            rankListModel.setRankNum(rankNum);
                            rankListModel.setRankList(RankMgr.getCrossActivityRankList(activityInfo.getActivityId(), dataEntry.getKey(), rankNum));
                            modelMap.put(dataEntry.getKey(), rankListModel);
                        }
                    }
                }
            }
        }
    }

    /**
     * 获取活动的榜单集合
     * @param activityId
     * @return
     */
    public static Map<Integer, RankListModel> getActivityRankListModelMap(int activityId) {
        return activityRankListModelMap.get(activityId);
    }

    /**
     * 获取活动的对应榜单
     * @param activityId
     * @param conditionType
     * @return
     */
    public static RankListModel getActivityRankListModel(int activityId, int conditionType) {
        Map<Integer, RankListModel> dataMap = getActivityRankListModelMap(activityId);
        if(null != dataMap){
            return dataMap.get(conditionType);
        }
        return null;
    }

    /**
     * 活动数据变动
     */
    public static void changeActivityRank(long userId, int activityId, int conditionType, BigInteger value, String param) {
        RankListModel rankListModel = getActivityRankListModel(activityId, conditionType);
        if (null == rankListModel) {
            return;
        }
        UserInfo userInfo = UserMgr.getUserInfo(userId);
        if(null == userInfo){
            return;
        }
        // 管理员账号不参与排行
        if (userInfo.isAdmin() || userInfo.getBlackListEndTime() > System.currentTimeMillis()) {
            return;
        }
        UserRank userRank = rankListModel.rankChange(userId, UserMgr.parseUserBaseInfo(userInfo, GameServer.getInstance().getServerId()), value, param);
        if(ActivityHelper.conditionTypeIsCrossRank(conditionType) && null != userRank){//需要上传跨服变更
            CrossDataMgr.activityRankChange(activityId, conditionType, userRank);
        }
    }

    /**
     * 需要跨服变更
     */
    private static boolean needSendCross(){
        //todo 增加需要上传跨服的逻辑判断
        return false;
    }
}
